package com.nbufe.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nbufe.common.constant.QueryPageParam;
import com.nbufe.common.constant.Result;
import com.nbufe.entity.CmsLog;
import com.nbufe.service.CmsLogService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ylz.java
 * @since 2023-03-22
 */
@RestController
@RequestMapping("/ylz/cms-log")
public class CmsLogController {

    @Autowired
    private CmsLogService cmsLogService;

    @GetMapping("/cms-log/list")
    @ApiOperation("查询所有的日志数据")
    public Result list() {
        return Result.suc(cmsLogService.list(null));
    }


    @GetMapping("/cms-log/findByDeviceid")
    @ApiOperation("找数据库中是否存在该id的日志数据")
    public Result findByDeviceid(@RequestParam String id) {
        QueryWrapper<CmsLog> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("id", id);
        List list = cmsLogService.list(wrapperChapter);
        return list.size() > 0 ? Result.suc(list) : Result.fail();
    }

    @PostMapping("/cms-log/save")
    @ApiOperation("新增一条日志数据")
    public Result save(@RequestBody CmsLog cmsLog) {
        return cmsLogService.save(cmsLog) ? Result.suc() : Result.fail();
    }


    @PostMapping("/cms-log/update")
    @ApiOperation("根据ID更新一条日志数据")
    public Result update(@RequestBody CmsLog cmsLog) {
        return cmsLogService.update(cmsLog, new QueryWrapper<CmsLog>().eq("id", cmsLog.getId())) ? Result.suc() : Result.fail();
//        return cmsLogService.updateById(cmsLog) ? Result.suc() : Result.fail();
    }

    @ApiOperation("删除一条日志数据")
    @GetMapping("/cms-log/del")
    public Result del(@RequestParam String id) {
        return cmsLogService.removeById(id) ? Result.suc() : Result.fail();
    }


    @PostMapping("/cms-log/listPage")
    @ApiOperation("分页模糊查询")
    public Result listPage(@RequestBody QueryPageParam query) {


        HashMap param = query.getParam();
        String id = (String) param.get("id");

        //当前页、每页多少条
        Page<CmsLog> page = new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());
        QueryWrapper<CmsLog> wrapperChapter = new QueryWrapper<>();
        if (StringUtils.isNotBlank(id) && !"null".equals(id)) {
            wrapperChapter.like("id", id);
        }

        IPage result = cmsLogService.page(page, wrapperChapter);
        return Result.suc(result.getRecords(), result.getTotal());
    }
}
